package properties;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Properties {
	
	private static Properties properties;

	private Document doc;

	private String user;
	private String password;
	private String dbms;
	private String url;
	private String dbName;
	private String host;
	private int port;
	private String consumerKey;
	private String consumerSecret;
	private String accessToken;
	private String accessTokenSecret;

	private Properties() {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(new File("properties.xml"));
			doc.getDocumentElement().normalize();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		//Get all the values from the xml file
		setValues();
	}
	
	/**
	 * Makes a singleton of properties. 
	 * @return
	 */
	public static Properties getProperties() { 
		if(properties == null) { 
			properties = new Properties();
		}
		return properties;
	}

	/**
	 * Read all the properties from the xml file.
	 */
	private void setValues() {
		NodeList list;

		list = doc.getElementsByTagName("user");
		this.user = list.item(0).getTextContent();

		list = doc.getElementsByTagName("password");
		this.password = list.item(0).getTextContent();

		list = doc.getElementsByTagName("dbms");
		this.dbms = list.item(0).getTextContent();

		list = doc.getElementsByTagName("url");
		this.url = list.item(0).getTextContent();

		list = doc.getElementsByTagName("dbname");
		this.dbName = list.item(0).getTextContent();

		list = doc.getElementsByTagName("host");
		this.host = list.item(0).getTextContent();

		list = doc.getElementsByTagName("port");
		this.port = Integer.parseInt(list.item(0).getTextContent());

		list = doc.getElementsByTagName("consumerkey");
		this.consumerKey = list.item(0).getTextContent();

		list = doc.getElementsByTagName("consumersecret");
		this.consumerSecret = list.item(0).getTextContent();

		list = doc.getElementsByTagName("accesstoken");
		this.accessToken = list.item(0).getTextContent();

		list = doc.getElementsByTagName("accesstokensecret");
		this.accessTokenSecret = list.item(0).getTextContent();
	}

	public String getUser() {
		return user;
	}

	public String getPassword() {
		return password;
	}

	public String getDbms() {
		return dbms;
	}

	public String getUrl() {
		return url;
	}

	public String getDbName() {
		return dbName;
	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public String getConsumerKey() {
		return consumerKey;
	}

	public String getConsumerSecret() {
		return consumerSecret;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public String getAccessTokenSecret() {
		return accessTokenSecret;
	}

}
